<!--
 * @页面名称:验证规则
 * @描述: 
 * @作者: 
 * @Date:
-->
<template>
  <div class="form-tab5">
    <!-- 按钮组 -->
    <div class="button-group">
      <el-col :span="6" class="input-search">
        <el-input placeholder="请输入关键字查询" v-model="searchString" @change="onSearchChange"></el-input>
      </el-col>
      <el-col :span="4">
        <el-tooltip class="box-item" placement="bottom-end">
          <template #content>
            <span>添加界面</span>
          </template>
          <el-button :disabled="BtnStatus[0]" :icon="FolderAdd" @click="add"></el-button>
        </el-tooltip>
        <el-tooltip class="box-item" placement="bottom-end">
          <template #content>
            <span>修改界面</span>
          </template>
          <el-button :disabled="BtnStatus[1]" :icon="Edit" @click="edit"></el-button>
        </el-tooltip>
        <el-tooltip class="box-item" placement="bottom-end">
          <template #content>
            <span>删除界面</span>
          </template>
          <el-button :disabled="BtnStatus[2]" :icon="Delete" @click="del"></el-button>
        </el-tooltip>
      </el-col>
    </div>
    <!-- 表格模块 -->
    <stable :columns="tableSetting.columns" :data="tableSetting.data" @rowClick="rowClick"></stable>
    <!-- 新增修改弹出框 -->
    <el-dialog v-model="dialogSetting.visible" :title="dialogSetting.title" width="30%" draggable>
      <el-form label-width="100px" :model="formData" ref="formRef">
        <el-form-item label="数据表字段:" prop="TableField">
          <el-select filterable v-model="formData.TableField">
            <el-option
              v-for="item in column"
              :key="item.TableId"
              :label="item.DDM_CNName + '(' + item.Field_Name + ')'"
              :value="item.Field_Name"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="验证函数名称:" prop="Method">
          <el-select v-model="formData.Method">
            <el-option v-for="item in ruleList" :key="item.label" :label="item.label" :value="item.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="函数触发方式:" prop="ControlType">
          <el-select v-model="formData.ControlType">
            <el-option v-for="item in ruleType" :key="item.key" :label="item.name" :value="item.key"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="验证值:" prop="ValidValue">
          <el-tooltip placement="top" content="判断是否为空时不用填写">
            <el-input v-model="formData.ValidValue"></el-input>
          </el-tooltip>
        </el-form-item>
        <el-form-item label="错误提示信息:" prop="Error">
          <el-input v-model="formData.Error" :autosize="{ minRows: 2, maxRows: 4 }" type="textarea"></el-input>
        </el-form-item>
        <el-form-item label="是否启用:" prop="Enable">
          <el-checkbox v-model="formData.Enable"></el-checkbox>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogSetting.visible = false">取消</el-button>
          <el-button type="primary" @click="handle"> 确定 </el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="tsx">
import { ref, reactive, nextTick } from "vue";
import { FolderAdd, Edit, Delete } from "@element-plus/icons-vue";
import useCurrentInstance from "@/utils/useCurrentInstance";
import { ruleList, ruleType } from "./type";
import type { FormInstance } from "element-plus";

// 按钮状态
const BtnStatus = ref([true, true, true]);
const { proxy } = useCurrentInstance();
// 搜索字段
const searchString = ref("");
// 父级表格保存对象
const tabClickFromParent = ref();
// 表格配置
const tableSetting = reactive({
  columns: [
    { title: "数据表字段", field: "V_TableField", sortable: true },
    { title: "验证函数名称", field: "V_Method", sortable: true },
    { title: "验证函数说明", field: "V_Remark", sortable: true },
    { title: "验证值(多个判断值用逗号隔开)", field: "V_ValidValue", sortable: true },
    { title: "错误提示", field: "V_Error", sortable: true },
    { title: "创建时间", field: "V_Create_Time", sortable: true }
  ],
  data: [] as any[],
  sourthTableData: [] as any[] //原始数据
});
// 表格单击保存对象
const clickObj = ref();
// 弹出框配置
const dialogSetting = reactive({
  visible: false,
  title: ""
});
// 表单绑定字段
const formData = reactive({
  TableField: "",
  Method: "",
  ValidValue: "",
  Error: "",
  ControlType: "",
  Enable: false
});
// 表单实例
const formRef = ref<FormInstance>();
// 数据表字段保存
const column = ref<any>([]);
// 获取表格数据
const getTableData = async (record: any) => {
  if (!record) {
    return;
  }
  tabClickFromParent.value = record;
  await getColumn();
  proxy.$vueAxios
    .post("Master_Base_Validation/GetConditional", {
      order: "v_Order asc",
      dtoList: [{ fieldName: "FMT_ID", fieldValue: record.FMT_ID }]
    })
    .then((res: any) => {
      tableSetting.sourthTableData = res.List;
      tableSetting.data = res.List;
      BtnStatus.value[0] = false;
    });
};
// 获取界面显示字段数据
const getColumn = async () => {
  await proxy.$vueAxios
    .post(
      "Master_Base_Data_Disp_Mstr/GetConditional",
      {
        order: "DDM_Order asc",
        dtoList: [{ fieldName: "FMT_ID", fieldValue: tabClickFromParent.value.FMT_ID }]
      },
      { noLoading: true }
    )
    .then((res: any) => {
      column.value = res.List;
    });
};
// 表格单击事件
const rowClick = ({ row }: any) => {
  BtnStatus.value[1] = false;
  BtnStatus.value[2] = false;
  clickObj.value = row;
};
// 新增事件
const add = () => {
  formRef.value?.resetFields();
  dialogSetting.visible = true;
  dialogSetting.title = "新增";
};
// 修改事件
const edit = () => {
  dialogSetting.visible = true;
  dialogSetting.title = "修改";
  nextTick(() => {
    formData.TableField = clickObj.value.V_TableField;
    formData.Method = clickObj.value.V_Method;
    formData.ValidValue = clickObj.value.V_ValidValue;
    formData.Error = clickObj.value.V_Error;
    formData.ControlType = clickObj.value.V_ControlType;
    formData.Enable = clickObj.value.V_Enable;
  });
};
// 删除事件
const del = () => {
  proxy.$messageBox
    .confirm("确定删除该数据？", "提示", {
      confirmButtonText: "确定",
      cancelButtonText: "取消",
      type: "warning"
    })
    .then(() => {
      proxy.$vueAxios.post("Master_Base_Validation/Delete", [clickObj.value.V_ID]).then((res: any) => {
        if (res.Mark) {
          tableSetting.data = tableSetting.data.filter(
            (v: any) => v !== tableSetting.data.find((v: any) => v.V_ID === clickObj.value.V_ID)
          );
          proxy.$message.success("删除成功");
        } else {
          proxy.$message.error("删除失败");
        }
      });
    })
    .catch(err => {
      console.log(err);
      proxy.$message("取消删除");
    });
};
// 新增修改确认事件
const handle = () => {
  formRef.value!.validate((isValid: boolean) => {
    if (isValid) {
      if (dialogSetting.title === "新增") {
        const param = {
          v_ID: proxy.$global.createID(),
          ddM_ID: column.value.filter((v: any) => v.Field_Name === formData.TableField)[0].DDM_ID,
          fmT_ID: tabClickFromParent.value.FMT_ID,
          v_TableField: formData.TableField,
          v_Method: formData.Method,
          v_ValidValue: formData.ValidValue,
          v_Error: formData.Error,
          v_ControlType: formData.ControlType,
          v_Enable: formData.Enable,
          v_Remark: formData.Method ? ruleList.filter((v: any) => v.value === formData.Method)[0].label.split("|")[1] : ""
        };
        proxy.$vueAxios.post("Master_Base_Validation", param).then(() => {
          dialogSetting.visible = false;
          getTableData(tabClickFromParent.value);
        });
      } else {
        const param = {
          v_ID: clickObj.value.V_ID,
          ddM_ID: clickObj.value.DDM_ID,
          fmT_ID: clickObj.value.FMT_ID,
          v_TableField: formData.TableField,
          v_Method: formData.Method,
          v_ValidValue: formData.ValidValue,
          v_Error: formData.Error,
          v_ControlType: formData.ControlType,
          v_Enable: formData.Enable,
          v_Remark: formData.Method ? ruleList.filter((v: any) => v.value === formData.Method)[0].label.split("|")[1] : ""
        };
        proxy.$vueAxios.put("Master_Base_Validation", param).then(() => {
          dialogSetting.visible = false;
          getTableData(tabClickFromParent.value);
        });
      }
    }
  });
};
// 搜索事件
const onSearchChange = () => {
  const filterData = tableSetting.sourthTableData.filter(data =>
    data.V_TableField.toLowerCase().includes(searchString.value.toLowerCase())
  );
  tableSetting.data = filterData;
};

// 将getTableData函数暴露给父组件
defineExpose({ getTableData });
</script>

<style scoped lang="scss">
.form-tab5 {
  height: calc(100% - 40px);
  .button-group {
    display: flex;
    margin: 0 0 10px;
    .input-search {
      margin-right: 10px;
    }
  }
}
</style>
